package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.service.AppService;
import com.ruoyi.bizsys.domain.AppUser;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.domain.LoanReq;
import com.ruoyi.common.config.ServerYml;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.ClassPropertyHandler;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: Jamnsin
 * @Date: 2021/3/10 2:24 下午
 */
@Slf4j
public class BaseController {

    @Autowired
    TokenService tokenService;

    @Autowired
    ICustomerInfoService customerInfoService;

    @Autowired
    ISysDictDataService sysDictDataService;

    @Autowired
    IAppUserService appUserService;

    @Autowired
    AppService appService;

    protected void checkAttribute(Object obj) {
        String errMsg = new ClassPropertyHandler<>(obj).hasNullOrEmptyPropertyValue();
        if (StringUtils.isNotBlank(errMsg)) {
            throw new BusinessException(errMsg);
        }
    }

    protected String getAppVersion() {
        String appVersion = ServletUtils.getRequest().getHeader("appVersion");
        if (Func.isEmpty(appVersion)) {
            throw new BusinessException(RspEnmu.APP_VERSION_DONOT_SUPPORT);
        }
        return appVersion;
    }

    protected int formatAppVersion(String appVersion) {
        return Integer.parseInt(appVersion.replaceAll("\\.", ""));
    }

    protected void checkTransPwd(String userId, String transPwd) {
        JSONObject transJson = new JSONObject();
        transJson.put("userId", getUserId());
        transJson.put("password", transPwd);
        appService.checkTranPassword(transJson);
    }

    protected void checkAttribute2(Object obj) {
        String errMsg = new ClassPropertyHandler<>(obj).hasNullOrEmptyValue();
        if (StringUtils.isNotBlank(errMsg)) {
            throw new BusinessException(errMsg);
        }
    }

    protected String getUserId() {
        String userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        if (StringUtils.isBlank(userId)) {
            log.warn("用户不存在或登录态失效");
            throw new BusinessException(RspEnmu.ILLEGAL_USER_REQ_ERR);
        }
        return userId;
    }

    protected String getUserId2() {
        try{
            String userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
            return userId;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 用户是否登录
     * @return
     */
    protected boolean getUserOnline() {
        LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
        if(user !=null) {
            return true;
        }
        return false;
    }

    /**
     * wqg修改，原来list.get(0)
     * @return
     */
    protected String getCustNo() {
        QueryWrapper<CustomerInfo> customerInfoWrapper = new QueryWrapper<>();
        customerInfoWrapper.eq("user_id",this.getUserId());
        customerInfoWrapper.eq("state",BizConstants.BIZ_STATUS_1);
        CustomerInfo customerInfo = customerInfoService.getOne(customerInfoWrapper);
        if (null==customerInfo) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }
        return customerInfo.getCustNo();
    }

    protected String getCustNo(String userId) {
        List<CustomerInfo> customerInfoList = getCustomerInfos(userId);
        return customerInfoList.get(0).getCustNo();
    }

    private List<CustomerInfo> getCustomerInfos(String userId) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setUserId(userId);
        customerInfo.setState(BizConstants.BIZ_STATUS_1);
        List<CustomerInfo> customerInfoList = customerInfoService.selectCustomerInfoList(customerInfo);
        if (CollectionUtils.isEmpty(customerInfoList)) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }
        return customerInfoList;
    }

    protected String getDictLabel(String dictType, String dictValue) {
        return sysDictDataService.selectDictLabel(dictType, dictValue);
    }

    protected boolean appGpReviewSwitch() {
        return Integer.valueOf(getDictLabel("biz_switch", "app_gp_in_review")) == 1;
    }

    protected AppUser getAppUser() {
        AppUser appUser = new AppUser();
        appUser.setUserUuid(this.getUserId());
        List<AppUser> appUsers = appUserService.selectAppUserList(appUser);
        if (CollectionUtils.isEmpty(appUsers)) {
            throw new BusinessException(RspEnmu.USER_NOT_EXIST);
        }
        return appUsers.get(0);
    }

    protected CustomerInfo getCustomerInfo() {
        return customerInfoService.getOne(new QueryWrapper<CustomerInfo>().eq("user_id", this.getUserId()));
    }


}
